document.addEventListener('DOMContentLoaded', function() {
    const cookieForm = document.getElementById('cookie-form');
    const userInfoDiv = document.getElementById('user-info');
    const verifyCookieBtn = document.getElementById('verify-cookie');

    // 页面加载时检查cookie状态
    async function checkCookieStatus() {
        try {
            const resp = await fetch('/api/cookie/status');
            const data = await resp.json();
            
            if (data.is_valid) {
                showUserInfo(data.user_info);
            } else {
                showInvalidCookie(data.message);
            }
        } catch (error) {
            console.error('检查Cookie状态失败:', error);
        }
    }

    // 显示用户信息
    function showUserInfo(userInfo) {
        if (userInfoDiv) {
            userInfoDiv.style.display = 'block';
            userInfoDiv.innerHTML = `
                <div class="alert alert-success">
                    <h4 class="alert-heading">Cookie状态：有效</h4>
                    <p>当前登录用户：${userInfo.username}</p>
                    <p>用户ID：${userInfo.uid}</p>
                </div>
            `;
        }
    }

    // 显示无效cookie信息
    function showInvalidCookie(message) {
        if (userInfoDiv) {
            userInfoDiv.style.display = 'block';
            userInfoDiv.innerHTML = `
                <div class="alert alert-danger">
                    <h4 class="alert-heading">Cookie状态：无效</h4>
                    <p>${message}</p>
                </div>
            `;
        }
    }

    // Cookie设置
    cookieForm.onsubmit = async function(e) {
        e.preventDefault();
        const cookie = document.getElementById('cookie').value.trim();
        if (!cookie) return;
        
        try {
            const resp = await fetch('/api/cookie', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({cookie})
            });
            const data = await resp.json();
            
            if (!resp.ok) {
                showInvalidCookie(data.error || '设置Cookie失败');
                return;
            }
            
            // 显示用户信息
            showUserInfo(data.user_info);
            
            // 清空输入框
            document.getElementById('cookie').value = '';
        } catch (error) {
            showInvalidCookie('设置Cookie失败: ' + error.message);
        }
    };

    // 验证Cookie
    if (verifyCookieBtn) {
        verifyCookieBtn.onclick = async function() {
            const cookie = document.getElementById('cookie').value.trim();
            if (!cookie) {
                showInvalidCookie('请输入Cookie后再验证');
                return;
            }

            try {
                const resp = await fetch('/api/cookie/verify', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams({cookie})
                });
                const data = await resp.json();
                
                if (resp.ok) {
                    showUserInfo(data.user_info);
                } else {
                    showInvalidCookie(data.error || '验证Cookie失败');
                }
            } catch (error) {
                showInvalidCookie('验证Cookie失败: ' + error.message);
            }
        };
    }

    // 页面加载时检查cookie状态
    checkCookieStatus();
}); 